<template>
  <div class="points-log">
    <el-card>
      <div class="header">
        <span></span>
      </div>
      <el-form :inline="true" :model="filter" class="filter-bar" label-width="90px" style="margin-bottom: 16px;">
        <el-form-item label="时间范围">
          <el-date-picker v-model="filter.date" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
        </el-form-item>
        <el-form-item label="用户信息">
          <el-input v-model="filter.user" placeholder="昵称/ID" clearable style="width: 220px;" />
        </el-form-item>
        <el-form-item label="变动类型">
          <el-select v-model="filter.type" multiple clearable placeholder="请选择" style="width: 220px;">
            <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户类型">
          <el-select v-model="filter.userType" clearable style="width: 220px;">
            <el-option label="B端" value="B" />
            <el-option label="C端" value="C" />
          </el-select>
        </el-form-item>
        <el-form-item label="来源/用途">
          <el-select v-model="filter.source" clearable style="width: 220px;">
            <el-option v-for="item in sourceOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <div class="filter-actions-row">
          <el-button type="primary" @click="doFilter">查询</el-button>
          <el-button @click="resetFilter">重置</el-button>
          <el-button @click="refresh">刷新</el-button>
          <el-button type="primary" @click="exportLog">导出</el-button>
        </div>
      </el-form>
      <div class="points-stat-bar">
        <div class="stat-item">
          <div class="stat-num">{{ stat.total }}</div>
          <div class="stat-label">总积分(个)</div>
        </div>
        <div class="stat-item">
          <div class="stat-num">{{ stat.signin }}</div>
          <div class="stat-label">签到送出积分(个)</div>
        </div>
        <div class="stat-item">
          <div class="stat-num">{{ stat.order }}</div>
          <div class="stat-label">购物赠送积分(个)</div>
        </div>
        <div class="stat-item">
          <div class="stat-num">{{ stat.admin }}</div>
          <div class="stat-label">操作员赠送积分(个)</div>
        </div>
        <div class="stat-item">
          <div class="stat-num">{{ stat.activity }}</div>
          <div class="stat-label">活动赠送积分(个)</div>
        </div>
        <div class="stat-item">
          <div class="stat-num stat-num-red">{{ stat.consume }}</div>
          <div class="stat-label">积分消耗(个)</div>
        </div>
      </div>
      <el-table :data="tableData" style="width: 100%" :default-sort="{prop: 'date', order: 'descending'}">
        <el-table-column prop="date" label="变动时间" width="160" sortable />
        <el-table-column prop="userId" label="用户ID" width="100" />
        <el-table-column prop="userName" label="昵称" width="120" />
        <el-table-column prop="userType" label="用户类型" width="80" />
        <el-table-column prop="type" label="变动类型" width="100" />
        <el-table-column prop="before" label="变动前积分" width="110" />
        <el-table-column prop="value" label="变动积分" width="110">
          <template #default="scope">
            <span :style="{color: scope.row.value > 0 ? '#67C23A' : '#F56C6C'}">{{ scope.row.value > 0 ? '+' : '' }}{{ scope.row.value }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="balance" label="变动后积分" width="110" />
        <el-table-column prop="effectTime" label="积分生效时间" width="160">
          <template #default="scope">
            <span v-if="scope.row.type === '增加'">{{ scope.row.effectTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="expire" label="积分有效期" width="140">
          <template #default="scope">
            <span v-if="scope.row.type === '增加'">{{ scope.row.expire }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="source" label="来源/用途" width="120" />
        <el-table-column prop="orderNo" label="关联单号" width="140">
          <template #default="scope">
            <el-link v-if="scope.row.orderNo" type="primary" :underline="false">{{ scope.row.orderNo }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="operator" label="操作人" width="100" />
        <el-table-column prop="remark" label="备注" min-width="120" />
      </el-table>
      <el-pagination
        v-model:current-page="page.current"
        v-model:page-size="page.size"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        style="margin-top: 16px; text-align: right;"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

const typeOptions = ['增加', '减少']
const sourceOptions = ['签到', '下单', '评价', '兑换', '抵现', '活动', '后台调整']

const filter = reactive({
  date: '' as string[] | string,
  user: '',
  type: [],
  userType: '',
  source: ''
})

interface PointsLogRow {
  date: string;
  userId: string;
  userName: string;
  userType: string;
  type: string;
  before: number;
  value: number;
  balance: number;
  expire: string;
  source: string;
  orderNo: string;
  operator: string;
  remark: string;
  effectTime: string;
}
const allData = ref<PointsLogRow[]>([
  {
    date: '2024-06-10 10:00:00', userId: '1001', userName: '张三', userType: 'C', type: '增加', before: 100, value: 50, balance: 150, expire: '2024-12-31', source: '签到', orderNo: '', operator: '系统', remark: '', effectTime: '2024-06-10 10:00:00'
  },
  {
    date: '2024-06-10 09:00:00', userId: '1002', userName: '李四', userType: 'B', type: '减少', before: 300, value: -100, balance: 200, expire: '', source: '抵现', orderNo: 'ORD20240610001', operator: '系统', remark: '订单抵扣', effectTime: ''
  },
  {
    date: '2024-06-09 18:00:00', userId: '1001', userName: '张三', userType: 'C', type: '增加', before: 80, value: 20, balance: 100, expire: '2024-12-31', source: '下单', orderNo: 'ORD20240609001', operator: '系统', remark: '', effectTime: '2024-06-09 18:00:00'
  },
  {
    date: '2024-06-08 12:00:00', userId: '1003', userName: '王五', userType: 'C', type: '减少', before: 110, value: -30, balance: 80, expire: '', source: '过期', orderNo: '', operator: '系统', remark: '年度清零', effectTime: ''
  },
  {
    date: '2024-06-07 15:00:00', userId: '1002', userName: '李四', userType: 'B', type: '增加', before: 100, value: 200, balance: 300, expire: '2024-12-31', source: '后台调整', orderNo: '', operator: '管理员', remark: '手动补发', effectTime: '2024-06-07 15:00:00'
  }
])

const page = reactive({ current: 1, size: 10, total: 0 })

const tableData = computed(() => {
  let data = allData.value
  // 筛选逻辑
  if (filter.date && filter.date.length === 2) {
    const [start, end] = filter.date
    data = data.filter(row => row.date >= start && row.date <= end)
  }
  if (filter.user) {
    data = data.filter(row => row.userId.includes(filter.user) || row.userName.includes(filter.user))
  }
  if (filter.type && filter.type.length) {
    data = data.filter(row => filter.type.includes(row.type))
  }
  if (filter.userType) {
    data = data.filter(row => row.userType === filter.userType)
  }
  if (filter.source) {
    data = data.filter(row => row.source === filter.source)
  }
  page.total = data.length
  const startIdx = (page.current - 1) * page.size
  return data.slice(startIdx, startIdx + page.size)
})

// 统计卡片数据，随tableData动态变化
const stat = computed(() => {
  let total = 0, signin = 0, order = 0, admin = 0, activity = 0, consume = 0
  tableData.value.forEach(row => {
    if (row.type === '增加') {
      total += row.value
      if (row.source === '签到') signin += row.value
      else if (row.source === '下单') order += row.value
      else if (row.source === '后台调整') admin += row.value
      else if (row.source === '活动') activity += row.value
    } else if (row.type === '减少') {
      total -= Math.abs(row.value)
      consume += Math.abs(row.value)
    }
  })
  return { total, signin, order, admin, activity, consume }
})

function doFilter() {
  page.current = 1
}
function resetFilter() {
  filter.date = ''
  filter.user = ''
  filter.type = []
  filter.userType = ''
  filter.source = ''
  page.current = 1
}
function refresh() {
  ElMessage.success('已刷新（mock）')
}
function exportLog() {
  ElMessage.success('导出成功（mock）')
}
</script>

<style scoped>
.points-log { padding: 32px; }
.points-stat-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  border-radius: 32px;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px 0 rgba(0,0,0,0.03);
  padding: 24px 0 12px 0;
}
.stat-item {
  flex: 1;
  text-align: center;
}
.stat-num {
  font-size: 36px;
  font-weight: bold;
  color: #222;
  line-height: 1.2;
}
.stat-label {
  font-size: 16px;
  color: #666;
  margin-top: 4px;
}
.stat-num-red {
  color: #F56C6C;
}
.header { display: flex; gap: 12px; align-items: center; margin-bottom: 12px; }
.filter-bar { margin-bottom: 16px; }
.filter-actions-row {
  margin-top: 8px;
  display: flex;
  gap: 12px;
}
</style> 